<?php

use Biz\model\brandActivity\BlindBoxActivityPrize as mBlindBoxActivityPrize;
use Biz\model\brandActivity\BlindBoxCueCard as mBlindBoxCueCard;
use Biz\model\brandActivity\BlindBoxLottery as mBlindBoxLottery;
use Biz\model\brandActivity\BlindBoxLotteryConsignee as mBlindBoxLotteryConsignee;
use Biz\service\brand\BlindBox;
use Biz\service\brand\Brand;
use Doraemon\service\User;
use Doraemon\tools\ImgUrl;

/**
 * 我的盲盒
 */
class MyBoxAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const PAGE_SIZE = 20;

    public function main()
    {
        $page = $this->getRequest()->getQuery('page', 1);
        $page = $page ? $page : 1;
        $where = [
            'uid' => $this->uid,
            'lottery_state[!]' => mBlindBoxLottery::STATUS_DELETE,
            'LIMIT' => [($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE],
            'ORDER' => ['addtime' => 'DESC'],
            'prize_id[!]' => BlindBox::EMPTY_BOX_PRIZE_ID
        ];
        $lotteryList = mBlindBoxLottery::selectByWhere($where);
        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = mBlindBoxLottery::selectCountWhere($where);
        $isOver = $page * self::PAGE_SIZE >= $count ? 1 : 0;
        $lotteryList = $lotteryList ? array_values($lotteryList) : [];
        $brandIds = $lotteryList ? array_column($lotteryList, 'brand_id') : [];
        $brandInfos = $brandIds ? Brand::getBrandListByIds4App($brandIds) : [];
        $list = [];
        $lotteryIds = array_column($lotteryList, 'id');
        foreach ($lotteryList as $row) {
            $brandId = $row['brand_id'];
            $brandInfo = ($brandId && isset($brandInfos[$brandId])) ? $brandInfos[$brandId] : null;
            $status = ($row['expire_time'] || time() < $row['expire_time']) ? 1 : 0;
            $list[] = [
                'lottery_id' => $row['id'],
                'prize_id' => $row['prize_id'],
                'prize_type' => $row['prize_type'],
                'prize_title' => $row['prize_title'],
                'prize_value' => $row['prize_value'],
                'prize_price' => $row['prize_price'],
                'prize_img' => $row['prize_img'],
                'brand_info' => $brandInfo,
                'status' => $status,//状态 0:已过期 1：正常 2：已使用:q!
                'expire_time' => $row['expire_time'] ? $row['expire_time'] : ''
            ];
            if ($row['prize_type'] == mBlindBoxActivityPrize::PRIZE_TYPE_CUE_CARD) {
                $lotteryCardIds[] = $row['prize_value'];
            }
        }
        $consigneeList = $lotteryIds ? mBlindBoxLotteryConsignee::selectByWhere(['lottery_id' => $lotteryIds]) : [];
        $consigneeList = $consigneeList ? array_column($consigneeList, null, 'lottery_id') : [];
        $cueCardList = $lotteryCardIds ? mBlindBoxCueCard::selectByWhere(['id' => $lotteryCardIds]) : [];
        $cueCardList = $cueCardList ? array_column($cueCardList, null, 'id') : [];
        foreach ($list as &$row) {
            $lotteryId = $row['lottery_id'];
            $prizeType = $row['prize_type'];
            $prizeValue = $row['prize_value'];
            switch ($prizeType) {
                case mBlindBoxActivityPrize::PRIZE_TYPE_GOODS:
                    if (isset($consigneeList[$lotteryId])) {
                        $row['consignees_info'] = [
                            'id' => $consigneeList[$lotteryId]['id'],
                            'express_status' => ($consigneeList[$lotteryId]['consignee_state'] == mBlindBoxLotteryConsignee::CONSIGNEE_SUCCESS) ? 1 : 0,//物流状态1:已签收 0:未签收
                        ];
                    }
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_SHOP_COUPON:
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_GOOD_COUPON:
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_JD_CARD:
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_CUE_CARD:
                    if (isset($cueCardList[$prizeValue])) {
                        $row['status'] = ($cueCardList[$prizeValue]['card_state'] == mBlindBoxCueCard::CARD_STATE_NORMAL) ? 1 : 2;
                    }
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_POINT:
                    $row['status'] = null;
                    break;
                case mBlindBoxActivityPrize::PRIZE_TYPE_LIMIT_CARD:
                    $row['prize_value'] = $row['prize_value'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $row['prize_value'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                    $row['status'] = 1;
                    break;
            }
        }
        $userInfo = User::getBasicUserInfo($this->uid);
        $this->setSuccess(['list' => $list, 'is_over' => $isOver,'user_info'=>$userInfo]);
    }
}